local jiuyuan = fk.CreateSkill {

  name = "joyex__jiuyuan$",

  tags = {  },

}



jiuyuan:addEffect(fk.PreHpRecover, {
  name = "joyex__jiuyuan$",
  anim_type = "support",
  events = {fk.PreHpRecover, fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if event == fk.PreHpRecover then
      return target ~= player and player:hasSkill(jiuyuan.name) and player:isWounded()
      and target.kingdom == "wu" and target == player.room.current
    else
      return target ~= player and player:hasSkill(jiuyuan.name) and player.dying
      and target.kingdom == "wu" and data.card.name == "peach"
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TargetSpecified then return true end
    return player.room:askForSkillInvoke(target, jiuyuan.name, nil, "#ex__jiuyuan-ask::"..player.id)
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.PreHpRecover then
      player.room:recover{ who = player, num = 1, skillName = jiuyuan.name, recoverBy = target }
      if not target.dead then
        target:drawCards(1, jiuyuan.name)
      end
      return true
    else
      data.additionalRecover = (data.additionalRecover or 0) + 1
    end
  end,
})
jiuyuan:addEffect(fk.TargetSpecified, {
  name = "joyex__jiuyuan$",
  anim_type = "support",
  events = {fk.PreHpRecover, fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if event == fk.PreHpRecover then
      return target ~= player and player:hasSkill(jiuyuan.name) and player:isWounded()
      and target.kingdom == "wu" and target == player.room.current
    else
      return target ~= player and player:hasSkill(jiuyuan.name) and player.dying
      and target.kingdom == "wu" and data.card.name == "peach"
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TargetSpecified then return true end
    return player.room:askForSkillInvoke(target, jiuyuan.name, nil, "#ex__jiuyuan-ask::"..player.id)
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.PreHpRecover then
      player.room:recover{ who = player, num = 1, skillName = jiuyuan.name, recoverBy = target }
      if not target.dead then
        target:drawCards(1, jiuyuan.name)
      end
      return true
    else
      data.additionalRecover = (data.additionalRecover or 0) + 1
    end
  end,
})

return jiuyuan